package com.neusmart.tongrentang.activity;

import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

import com.neusmart.tongrentang.F;
import com.neusmart.tongrentang.R;
import com.neusmart.tongrentang.constants.API;
import com.neusmart.tongrentang.constants.Key;
import com.neusmart.tongrentang.dataLoadRequest.DataLoadActivity;
import com.neusmart.tongrentang.model.DeviceEnvironment;
import com.neusmart.tongrentang.model.MParam;
import com.neusmart.tongrentang.result.ResultGetDeviceEnvironment;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by keven on 2016/8/15.
 */
public class StartActivity extends DataLoadActivity {

    private boolean isFirstIn;//判断app是不是安装第一次启动
    //
    private RelativeLayout mStartPage;
    private Animation mFadeIn;

    private DeviceEnvironment deviceEnvironment;
    private String serviceUrl;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_start;
    }

    @Override
    protected void init() {
        initView();
        setListener();
        checkDeviceEnv();//检查设备环境
    }

    private void initView() {
        isFirstIn = getBoolean(Key.IS_FIRST_IN, true);
        mStartPage = (RelativeLayout) findViewById(R.id.start_page);
        mFadeIn = AnimationUtils.loadAnimation(this, R.anim.welcome_fade_in);
        mStartPage.startAnimation(mFadeIn);
    }

    private void setListener() {
        mFadeIn.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                //
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                switch2Next();
            }
        });
    }

    protected void checkDeviceEnv(){
        loadData(API.DEVICE_ENVIRONMENT_GET, false);
    }

    @Override
    protected void initParams(MParam param) {
        switch (param.getApi()) {
            case DEVICE_ENVIRONMENT_GET:
                param.addParam("deviceId", F.deviceId);
                break;
            default:
                break;
        }
        loadData(param);
    }

    //判断是不是第一次启动app，是的就跳转到引导页，不是就直接跳转到首页
    private void switch2Next() {
        switchActivityAndFinish(isFirstIn ? GuideActivity.class : MainActivity.class, null);
    }

    @Override
    protected void disposeResult(API api, String response) {
        if (response == null)
            return;
        switch (api) {
            case DEVICE_ENVIRONMENT_GET:
                ResultGetDeviceEnvironment resultGetDeviceEnvironment = fromJson(response, ResultGetDeviceEnvironment.class);
                if (!resultGetDeviceEnvironment.isSuccess())
                    return;
                if(resultGetDeviceEnvironment.getData() != null){
                    deviceEnvironment = resultGetDeviceEnvironment.getData();
                    serviceUrl = deviceEnvironment.getServiceUrl();
                    if(serviceUrl != null && serviceUrl != ""){
                        F.domain = serviceUrl;
                    }
                }
            default:
                break;
        }
    }

    @Override
    protected boolean showNetworkConnection(boolean isConnected) {
        return isConnected;
    }

    @Override
    public void onBackPressed() {
        //
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

}
